"""
资金服务
"""

from jlpay.services.base_service import BaseService
from jlpay.model.fund_deferred import (
    DeferredActivationQueryRequest, DeferredActivationQueryResponse,
    DeferredActivationOpenRequest, DeferredActivationOpenResponse,
    DeferredActivationCloseRequest, DeferredActivationCloseResponse,
    FundFlowQueryRequest, FundFlowQueryResponse,
    FundFreezeRequest, FundFreezeResponse,
    FundUnfreezeRequest, FundUnfreezeResponse,
    FundRefreezeRequest, FundRefreezeResponse
)


class FundService(BaseService):
    """资金服务类"""
    
    def __init__(self, config):
        """
        初始化资金服务
        
        Args:
            config: OrgConfig实例
        """
        super().__init__(config)
    
    def get_api_url(self, request_class, api_path=None):
        """
        获取API URL
        
        Args:
            request_class: 请求类
            api_path: 接口路径
            
        Returns:
            完整的API URL
        """
        base_url = self.config.url
        
        if api_path:
            return f"{base_url}{api_path}"
        
        # 使用Request类中定义的API_PREFIX和API_URI
        api_prefix = getattr(request_class, 'API_PREFIX', '')
        api_uri = getattr(request_class, 'API_URI', '')
        
        if not api_uri:
            # 如果没有定义API_URI，使用默认路径
            return f"{base_url}/open/fund/deferred/activation"
        
        # 组合API路径
        path = api_prefix.lstrip('/') + '/' + api_uri.lstrip('/')
        return f"{base_url}/{path}"
    
    def query_deferred_activation(self, request):
        """
        查询资金管控状态
        
        Args:
            request: DeferredActivationQueryRequest实例
            
        Returns:
            DeferredActivationQueryResponse实例
        """
        return self.post(request, DeferredActivationQueryResponse)
    
    def open_deferred_activation(self, request):
        """
        开通资金管控
        
        Args:
            request: DeferredActivationOpenRequest实例
            
        Returns:
            DeferredActivationOpenResponse实例
        """
        return self.post(request, DeferredActivationOpenResponse)
    
    def close_deferred_activation(self, request):
        """
        关停资金管控
        
        Args:
            request: DeferredActivationCloseRequest实例
            
        Returns:
            DeferredActivationCloseResponse实例
        """
        return self.post(request, DeferredActivationCloseResponse)
    
    def query_fund_flow(self, request):
        """
        查询资金流水
        
        Args:
            request: FundFlowQueryRequest实例
            
        Returns:
            FundFlowQueryResponse实例
        """
        return self.post(request, FundFlowQueryResponse)
    
    def freeze_fund(self, request):
        """
        冻结资金
        
        Args:
            request: FundFreezeRequest实例
            
        Returns:
            FundFreezeResponse实例
        """
        return self.post(request, FundFreezeResponse)
    
    def unfreeze_fund(self, request):
        """
        解冻资金
        
        Args:
            request: FundUnfreezeRequest实例
            
        Returns:
            FundUnfreezeResponse实例
        """
        return self.post(request, FundUnfreezeResponse)
    
    def refreeze_fund(self, request):
        """
        续冻资金
        
        Args:
            request: FundRefreezeRequest实例
            
        Returns:
            FundRefreezeResponse实例
        """
        return self.post(request, FundRefreezeResponse) 